package ru.spies.profileClasses;

import ru.spies.forms.EDBFunctional;
import ru.spies.forms.EDBTables;
import ru.spies.forms.ESystemFunction;


public class UserPrivileges {
	
	private boolean privilegesTable[][]=null;
	private boolean additionalPrivileges[]=null;
	private boolean minimumUpdate;
	private boolean minimumShow;
	private boolean minimumDelete;
	private static int rowsSize;
	private static int colsSize;
	private static int additionalCount;
	
	private UserPrivileges(boolean privilegesTable[][],boolean additionalPrivileges[]) {
		this.privilegesTable=privilegesTable;
		this.additionalPrivileges=additionalPrivileges;
		this.minimumUpdate=true;
		this.minimumShow=true;
		this.minimumDelete=true;
	}
	
	public static UserPrivileges createUserWithAllPrivileges(){
		startInit();
		boolean privilegesTable[][]=new boolean[rowsSize][colsSize];
		for (int i = 0; i < rowsSize; i++) {
			for (int j = 0; j < colsSize; j++) {
				privilegesTable[i][j]=true;
			}
		}
		boolean additionalPrivileges[]= new boolean [additionalCount];
		for (int i = 0; i < additionalPrivileges.length; i++) {
			additionalPrivileges[i]=true;
		}
		UserPrivileges retVal=new UserPrivileges(privilegesTable,additionalPrivileges);
		retVal.setAllPrivileges();
		return retVal;
	}
	
	public static UserPrivileges createUserWithoutPrivileges(){
		startInit();
		boolean privilegesTable[][]=new boolean[rowsSize][colsSize];
		for (int i = 0; i < rowsSize; i++) {
			for (int j = 0; j < colsSize; j++) {
				privilegesTable[i][j]=false;
			}
		}
		boolean additionalPrivileges[]= new boolean [additionalCount];
		for (int i = 0; i < additionalPrivileges.length; i++) {
			additionalPrivileges[i]=false;
		}
		return new UserPrivileges(privilegesTable,additionalPrivileges);
	}
	
	public static UserPrivileges createUserWithStandartPrivilages(){
		UserPrivileges privil=createUserWithoutPrivileges();
		privil.setStandartPrivileges();
		return privil;
	}
	
	public static UserPrivileges createUserWithResidentPrivilages(){
		UserPrivileges privil=createUserWithoutPrivileges();
		privil.setStandartPrivileges();
		privil.setResidentPrivileges();
		return privil;
	}
	
	private void addPrivilege(EDBTables table,ESystemFunction function){
		this.privilegesTable[function.ordinal()][table.ordinal()]=true;
	}
	
	private void addAddionalPrivilege(EDBFunctional function){
		this.additionalPrivileges[function.ordinal()]=true;
	}
	
	private static void startInit(){
		rowsSize=ESystemFunction.values().length-1;
		colsSize=EDBTables.values().length;
		additionalCount=EDBFunctional.values().length;
	}
	
	public boolean getPrivilageStatus(EDBTables table,ESystemFunction function){
		return this.privilegesTable[function.ordinal()][table.ordinal()];
	}
	
	public boolean getAdditionalPrivilegeStatus(EDBFunctional function){
		return this.additionalPrivileges[function.ordinal()];
	}
	
	private void setStandartPrivileges(){
		this.addPrivilege(EDBTables.DOCUMENT, ESystemFunction.SHOW_OBJECTS);
		this.addPrivilege(EDBTables.TASK, ESystemFunction.SHOW_OBJECTS);
		this.addPrivilege(EDBTables.DEPARTMENT, ESystemFunction.SHOW_OBJECTS);
		this.addPrivilege(EDBTables.TASK_TYPE, ESystemFunction.SHOW_OBJECTS);
		this.addPrivilege(EDBTables.AGENT, ESystemFunction.SHOW_OBJECTS);
		this.addPrivilege(EDBTables.AGENT_AND_TASK, ESystemFunction.SHOW_OBJECTS);
		this.addPrivilege(EDBTables.IMPORTANCE, ESystemFunction.SHOW_OBJECTS);
		this.addPrivilege(EDBTables.COUNTRY, ESystemFunction.SHOW_OBJECTS);
		this.addPrivilege(EDBTables.DOCUMENT, ESystemFunction.ADD_NEW_TABLE_OBJECT);
		this.addPrivilege(EDBTables.DOCUMENT, ESystemFunction.UPDATE_OBJECT);
		this.addPrivilege(EDBTables.DOCUMENT, ESystemFunction.DELETE_OBJECT_FROM_TABLE);
		this.addPrivilege(EDBTables.AGENT, ESystemFunction.UPDATE_OBJECT);
		this.addAddionalPrivilege(EDBFunctional.SHOW_USERS);
		this.addAddionalPrivilege(EDBFunctional.GET_DOCUMENTS_BY_IMPORTANCE);
		this.addAddionalPrivilege(EDBFunctional.GET_POPULAR_DEPARTMENT);
		this.addAddionalPrivilege(EDBFunctional.IS_COUNTRY_EXISTS);
		this.addAddionalPrivilege(EDBFunctional.TOP_AGENTS);
	}
	
	private void setResidentPrivileges(){
		this.addPrivilege(EDBTables.AGENT, ESystemFunction.DELETE_OBJECT_FROM_TABLE);
		this.addPrivilege(EDBTables.AGENT, ESystemFunction.ADD_NEW_TABLE_OBJECT);
		this.addPrivilege(EDBTables.AGENT_AND_TASK, ESystemFunction.UPDATE_OBJECT);
		this.addPrivilege(EDBTables.AGENT_AND_TASK, ESystemFunction.ADD_NEW_TABLE_OBJECT);
		this.addPrivilege(EDBTables.AGENT_AND_TASK, ESystemFunction.DELETE_OBJECT_FROM_TABLE);
		this.addPrivilege(EDBTables.TASK, ESystemFunction.UPDATE_OBJECT);
		this.addPrivilege(EDBTables.TASK, ESystemFunction.ADD_NEW_TABLE_OBJECT);
	}
	
	private void setAllPrivileges(){
		this.setMinimunResidentPrivileges();
		this.minimumDelete=false;
		this.minimumShow=false;
	}
	
	private void setMinimunResidentPrivileges(){
		this.minimumUpdate=false;
	}
	
	public boolean canDoNormalUpdate(){
		if (!minimumUpdate){
			return true;
		}
		return false;
	}
	
	public boolean canDoNormalShow(){
		if (!minimumShow){
			return true;
		}
		return false;
	}
	
	public boolean canDoNormalDelete(){
		if (!minimumDelete){
			return true;
		}
		return false;
	}
		
}
